#include "ccd290_extract.h"


int main( int argc, char *argv[] ) {

    ccd290_config cfg;

	int opt;
    opterr = 1;

    int line_start=0, line_end=0;
    char in_file[128];
    char out_file[128];
    char conf_file[128];
    
    int line_start_set = 0;
    int line_end_set = 0;
    int in_file_set = 0;
    int out_file_set = 0;
    int has_conf_file = 0;

    while( (opt=getopt(argc,argv,"s:e:f:o:c:")) != -1 ){
        switch(opt){
            case 's':
                line_start = atoi(optarg);
                line_start_set = 1;
                printf("--> line_start = %6d\n", line_start);
                break;
            case 'e':
                line_end = atoi(optarg);
                line_end_set = 1;
                printf("--> line_end   = %6d\n", line_end);
                break;
            case 'f':
                sprintf(in_file, "%s", optarg);
                in_file_set = 1;
                printf("--> getting rows from : %s\n", in_file);
                break;
            case 'o':
                sprintf(out_file, "%s", optarg);
                out_file_set = 1;
                printf("--> output rows into : %s\n", out_file);
                break;
            case 'c':
                sprintf(conf_file, "%s", optarg);
                has_conf_file = 1;
                printf("--> load parameters from config file: %s\n", conf_file);
                break;
            default:
                printf("usage: %s -s line-start -e line_end -f input.bin -o out.txt\n", argv[0]);
        };
    }

    if( has_conf_file == 1 ){}
        if( has_conf_file == 1 ){
        init_ccd290_config_from_file( &cfg, conf_file );
    } else {
        init_ccd290_config_from_file( &cfg, "ccd290_config.ini" );
        // init_ccd290_config_to_defaults( &ccd290_cfg );
    }

    int opt_is_ok = line_start_set && line_end_set && in_file_set && out_file_set;
    
    if( !opt_is_ok ){
        if( !line_start_set )
            printf("opt line_start is not provided.\n");

        if( !line_end_set )
            printf("opt line_end is not provided.\n");

        if( !in_file_set )
            printf("opt in_file is not provided.\n");

        if( !out_file_set )
            printf("opt out_file is not provided.\n");
            
        return 0;
    }
    
    if( line_end <= line_start ){
        printf("-> ERROR: line_end MUST be larger than line_start!\n");
        return 0;
    }

    // if( cfg.BIT_SHIFT == 0 ){
    // 	printf("--> No 4-bit shift.\n");
    // 	get_rows_ob_tag_no_shift(in_file, out_file, line_start, line_end, &cfg);
    // }
    // else if( cfg.BIT_SHIFT == 1 ){
    // 	printf("--> Has 4-bit shift.\n");
    //     get_rows_ob_tag(in_file, out_file, line_start, line_end, &cfg);
    // }

    

    return 0;

}
